"use strict";
var wxCharts = require("./wxcharts.js");
export default {
	/**
	 * 显示加载框
	 * @param {显示标题} title
	 */
	showLoading(title) {
		title = title || ' ';
		uni.showLoading({
			title: title
		});
	},
	/**
	 * 隐藏加载框
	 * delay 延迟时间毫秒
	 */
	hideLoading() {
		uni.hideLoading();
	},

	/*显示Toast消息*/
	showToast(msg, fn) {
		uni.showToast({
			title: msg,
			icon: "none"
		})
		if (fn) {
			setTimeout(() => {
				fn();
			}, 2000);
		}
	},

	/**
	 * 必须输入验证输入
	 * */
	validate(txtinput, field, msg = "") {
		if (txtinput.length == 0) {
			this.showToast(`${field}${msg}`);
			return false;
		}
		return true;
	},

	/**页面跳转
	 * @param {url} url
	 * @param {数据} data
	 */
	pageTo(url, data) {
		url = '/pages/' + url;
		if (data) {
			uni.navigateTo({
				url: url + "?item=" + encodeURIComponent(JSON.stringify(data))
			})
		} else {
			uni.navigateTo({
				url
			})
		}
	},

	/*回转*/
	navigateBack(delta) {
		if(delta){
			uni.navigateBack({ delta })
		}else{
			uni.navigateBack()
		}
	},

	/**重定向
	 * @param {url} url
	 * @param {数据} data
	 */
	redirectTo(url, data) {
		url = '/pages/' + url;
		if (data) {
			uni.redirectTo({
				url: url + "?item=" + encodeURIComponent(JSON.stringify(data))
			})
		} else {
			uni.redirectTo({
				url
			})
		}
	},
	
	/**Tab跳转
	 * @param {url} url
	 * @param {数据} data
	 */
	switchTo(url, data) {
		uni.switchTab({
			url: '/pages/' + url
		})
	},
	
	reLaunch(url, data) {
		uni.reLaunch({
			url: '/pages/' + url,
		})
	},

	/*设置页面标题*/
	setPageTitle(title) {
		uni.setNavigationBarTitle({
			title,
		});
	},
	
	showConfirm(title, msg, fn) {
		uni.showModal({
			title: title,
			content: msg,
			success: function(res) {
				fn(res.confirm);
			}
		});
	},
	
	/* 弹出确认框 */
	showModal(title,content,confirm,cancelText,confirmText,showCancel){
		uni.showModal({
			title,
		    content,
			cancelText,
			confirmText,
			showCancel,
		    success: function (res) {
		        if (res.confirm) {
		            confirm&&confirm();
		        } else if (res.cancel) {
		           // cancel&&cancel();
		        }
		    }
		});
	},
	/**
	 * @param {Object} el
	 */
	vibrate(){
		uni.vibrate({
		    success: function () {
		        console.log('success');
		    }
		});
	},
	
	
	/**
	 * 获取Scroll元素高度
	 * */
	getScrollHeight(el) {
		return new Promise((resolve, reject) => {
			uni.getSystemInfo({
				success(res) {
					let pH = res.windowHeight
					let content = uni.createSelectorQuery().select(el);
					content.boundingClientRect(data => {
						resolve(pH - data.top);
					}).exec();
				},
				fail() {
					reject(0);
				}
			})
		});
	},
	
	/*
		 *显示饼图
		 * chartid 图表ID
		 * data 饼图数据
		 * width 图表显示宽度
		 */
		showChartPie(chartid, data, width) {
			data = data || [];
			if (data.length <= 0) {
				data.push({
					name: '没有数据',
					data: 0
				});
			}
			let Charts = new wxCharts({
				canvasId: chartid,
				type: 'pie',
				fontSize: 11,
				background: '#FFFFFF',
				animation: true,
				series: data,
				width: width,
				height: 280,
				dataLabel: true,
				pixelRatio: 1,
			});
		},
	
		/*
		 * 显示折线图
		 * chartid 图表ID
		 * categories 组
		 * series 数据
		 * width 图表显示宽度
		 */
		showChartLine(chartid, chartData,height,width) {
			console.log('chartid',chartid)			console.log('chartData',chartData)			if (chartData.categories.length <= 0) {				chartData.categories.push('');			}			chartData.series[0].data = chartData.series[0].data || [];			if (chartData.series[0].data.length <= 0) {				chartData.series[0].name = "没有数据";				chartData.series[0].data.push(0);			}
	
			let Charts = new wxCharts({
				canvasId: chartid,
				type: 'area',
				legend: false,
				fontSize: 11,
				background: '#FFFFFF',
				animation: false,
				yAxis: {   //是否隐藏x轴分割线
					disableGrid: true,
					format: function (val) {
							  return val.toFixed(1);
							},
				  },
				series: chartData.series,
				categories: chartData.categories,
				width: width,
				height: height,
				pixelRatio: 1,
			});
		},
	
		/**
		 * 显示圆弧图
		 *  chartid 图表ID
		 * chartData 数据
		 * */
		showChartArcbar(chartid, chartData, pixelRatio, width, height) {
			let Charts = new wxCharts({
				canvasId: chartid,
				type: 'arcbar',
				fontSize: 11,
				legend: false,
				title: {
					name: chartData.series[0].data * 100 + '%',
					color: '#667766',
					fontSize: 10
				},
				subtitle: {
					name: chartData.series[0].name,
					color: '#666666',
					fontSize: 10
				},
				extra: {
					arcbar: {
						type: 'default', //整圆类型进度条图
						width: 10,
						startAngle: 0, //整圆类型只需配置起始角度即可
						backgroundColor: '#ffe3e8',
					},
					ringChart: 1000
				},
				background: '#FFFFFF',
				pixelRatio: pixelRatio,
				series: chartData.series,
				animation: true,
				width: width * pixelRatio,
				height: height * pixelRatio,
				dataLabel: true,
			});
		},
	
		/**
		 * 显示柱状图
		 * chartid 图表ID
		 * chartData 数据
		 * */
		showChartColumn(chartid, chartData) {
			console.log('chartid',chartid)
			console.log('chartData',chartData)
			if (chartData.categories.length <= 0) {
				chartData.categories.push('');
			}
			chartData.series[0].data = chartData.series[0].data || [];
			if (chartData.series[0].data.length <= 0) {
				chartData.series[0].name = "没有数据";
				chartData.series[0].data.push(0);
			}
			let Charts = new wxCharts({
				canvasId: chartid,
				type: 'column',
				legend: true,
				fontSize: 10,
				background: '#FFFFFF',
				animation: true,
				categories: chartData.categories,
				series: chartData.series,
				width: 350,
				height: 250,
				pixelRatio: 1,
				rotate: false, //横屏模式
				xAxis: {
					disableGrid: false,
				},
				yAxis: {
					disabled: false,
				},
				dataLabel: true,
				extra: {
					column: {
						width: 40
					}
				}
			});
			return Charts;
		},
}
